/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package Wires.impl;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.ecore.EClass;

import Wires.CompositeTransformation;
import Wires.ConnectableElement;
import Wires.InputActualParameter;
import Wires.InputFormalParameter;
import Wires.OutputActualParameter;
import Wires.WiresPackage;
import Wires.exep.TypeNotFoundException;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Composite Transformation</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class CompositeTransformationImpl extends TransformationImpl implements CompositeTransformation {
	
	/***************************************************/
	/***************** Modifications *******************/
	
	public void execute(ConnectableElement incoming) throws Exception{
		
		List<InputActualParameter> inparam = this.getInParams();
		List<InputFormalParameter> formalInParameter = new LinkedList<InputFormalParameter>();
		
		boolean ready = true;
		for (InputActualParameter parameter : inparam){
			if (parameter.getType() == null)
				throw new TypeNotFoundException(parameter);
			ready = ready && ((InputActualParameterImpl)parameter).getActive();
			((InputFormalParameterImpl) parameter.getType()).basicDataValue = ((InputActualParameterImpl)parameter).basicDataValue;
			((InputFormalParameterImpl) parameter.getType()).metamodel = ((InputActualParameterImpl)parameter).metamodel;
			((InputFormalParameterImpl) parameter.getType()).model = ((InputActualParameterImpl)parameter).model;
			((InputFormalParameterImpl) parameter.getType()).modelPath = ((InputActualParameterImpl)parameter).modelPath;			
			formalInParameter.add((InputFormalParameter) parameter.getType());
		}					
				
		if (super.getActive() == true && ready){
			//Make a reference between the formal and the actual parameter
			for(OutputActualParameter p : this.getOutParams()){
				if (p.getType() == null)
					throw new TypeNotFoundException(p);
				((OutputFormalParameterImpl)p.getType()).actualParameter = p;
			}							
			
			List<ConnectableElement> list = new LinkedList<ConnectableElement>();
			for (InputFormalParameter p : formalInParameter ){				
				list.add(p);
			}
			
			WiresSpecificationImpl.execute(list, null);
			
			for (InputActualParameter iap : this.getInParams()){
				((InputActualParameterImpl) iap).setActive(false);
			}
			
			if (this.getControlNode()!=null)
				super.setActive(false);
		}
		
	}
	
	/**************** Modifications End ****************/
	/***************************************************/
	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected CompositeTransformationImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return WiresPackage.Literals.COMPOSITE_TRANSFORMATION;
	}

} //CompositeTransformationImpl
